package com.module.app.pop

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.annotation.StringRes
import androidx.lifecycle.lifecycleScope
import com.module.app.ext.updateFlow
import com.module.base.utils.LogUtils
import com.lxj.xpopup.XPopup
import com.module.app.R
import com.module.frame.ext.observe

class ProgressPop(builder: com.module.app.widget.dialog.AlertBuilder) : com.module.app.pop.CommonPop(builder) {
    companion object
    class Builder(context: Context?) {
        private var mController: com.module.app.widget.dialog.AlertBuilder =
            com.module.app.widget.dialog.AlertBuilder()
        fun setTitle(@StringRes titleId: Int): Builder {
            mController.title = mController.context.resources.getString(titleId)
            return this
        }

        fun setTitle(title: CharSequence?): Builder {
            mController.title = title
            return this
        }

        fun setProgressTitle(@StringRes titleId: Int): Builder {
            mController.progressTitle = mController.context.resources.getString(titleId)
            return this
        }

        fun setProgressTitle(title: CharSequence?): Builder {
            mController.progressTitle = title
            return this
        }

        fun setContent(@StringRes titleId: Int): Builder {
            mController.content = mController.context.resources.getString(titleId)
            return this
        }

        fun setContent(title: CharSequence?): Builder {
            mController.content = title
            return this
        }

        fun setCancelButton(@StringRes titleId: Int): Builder {
            mController.cancelText = mController.context.resources.getString(titleId)
            return this
        }

        fun setCancelButton(title: CharSequence?): Builder {
            mController.cancelText = title
            return this
        }

        fun setConfirmButton(@StringRes titleId: Int): Builder {
            mController.confirmText = mController.context.resources.getString(titleId)
            return this
        }

        fun setConfirmButton(title: CharSequence?): Builder {
            mController.confirmText = title
            return this
        }

        fun setCancelable(cancelable: Boolean): Builder {
            mController.mCancelable = cancelable
            return this
        }

        fun setAutoDismiss(isAutoDismiss: Boolean): Builder {
            mController.isAutoDismiss = isAutoDismiss
            return this
        }

        fun setCanceledOnTouchOutside(mCanceledOnTouchOutside: Boolean): Builder {
            mController.mCanceledOnTouchOutside = mCanceledOnTouchOutside
            return this
        }

        fun setOnCancelListener(onCancelListener: OnClickListener?): Builder {
            mController.mOnCancelListener = onCancelListener
            return this
        }

        fun setOnConfirmListener(mOnConfirmListener: OnClickListener?): Builder {
            mController.mOnConfirmListener = mOnConfirmListener
            return this
        }

        /**
         * @see android.view.Gravity
         */
        fun setContentGravity(gravity: Int): Builder {
            mController.gravity = gravity
            return this
        }

        fun create(): ProgressPop {
            return XPopup.Builder(mController.context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .dismissOnBackPressed(mController.mCancelable)
                .dismissOnTouchOutside(mController.mCanceledOnTouchOutside)
                .asCustom(ProgressPop(mController)) as ProgressPop
        }

        fun show() {
            create().show()
        }

        fun show(delayMillis: Long) {
            Handler(Looper.getMainLooper()).postDelayed(Runnable {
                if (mController.context == null || mController.context is Activity && (mController.context as Activity).isFinishing) {
                    return@Runnable
                }
                create().show()
            }, delayMillis)
        }

        init {
            mController.context = context
        }
    }

    var layoutContent: View? = null
    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_progress
    }

    override fun onCreate() {
        super.onCreate()
        layoutContent = findViewById(R.id.layout_content)
        observe(lifecycleScope, this, updateFlow) {
            try {
                if (it.status == com.module.app.bean.IType.STATUS.start) {
                    setShowProgress()
                }
            } catch (e: Exception) {
                LogUtils.e(e.message)
                e.printStackTrace()
            }
        }
    }

    private fun setShowProgress() {
        dismiss()
        ProgressPop2.show(context, builder.progressTitle)
    }

    override fun dismiss() {
        super.dismiss()
    }
}